package project;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void studentSystem() {
        //创建学生集合
        ArrayList<Student> list = new ArrayList<>();
        pool:while (true){
            System.out.println("-------------欢迎来到黑马学生管理系统----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch (choose){
                case "1" :
                    addStudent(list);
                    continue ;
                case "2" :
                    deleteStudent(list);
                    continue ;
                case "3" :
                    updateStudent(list);
                    continue ;
                case "4" :
                    queryStudent(list);
                    continue ;
                case "5" :
                    System.out.println("退出");
                    break pool;
//                    System.exit(0);//停止虚拟机运行
                default:
                    System.out.println("没有这个选项");
            }
        }
    }

    //添加学生
    public static void addStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生id:");
        String id = sc.next();
        while (true){
            //判断id是否存在
            if (contains(list,id)){//存在重新输入
                System.out.println("学生id已存在，请重新输入!");
                System.out.println("请重新输入学生id:");
                id = sc.next();
            } else {//不存在:结束循环
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        int age = sc.nextInt();
        System.out.println("请输入学生家庭住址:");
        String homeAddress = sc.next();
        //创建学生对象
        Student stu = new Student(id,name,age,homeAddress);
        //将学生添加到列表中
        list.add(stu);

        //提示用户添加成功!
        System.out.println("学生添加成功！");
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的id:");
        String id = sc.next();
        //判断id，获取索引
        int index = getIndex(list,id);
        if (index == -1){//不存在
            System.out.println("删除失败,id不存在!");
        } else {//存在--删除学生
            list.remove(index);
        }
    }

    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的学生id:");
        String id = sc.next();
        //获取id的索引
        int index = getIndex(list,id);
        //判断id是否存在
        if (index == -1){//不存在
            System.out.println("修改失败,id存在");
        } else {//存在--修改学生信息
            System.out.println("请输入要修改的学生姓名:");
            String name = sc.next();
            System.out.println("请输入要修改的学生年龄:");
            int age = sc.nextInt();
            System.out.println("请输入要修改的家庭住址:");
            String homeAddress = sc.next();
            Student stu = new Student(id,name,age,homeAddress);
            //修改列表中的学生信息
            list.set(index,stu);
        }
    }

    //查询学生
    public static void queryStudent(ArrayList<Student> list){
        if (list.size() == 0){
            System.out.println("当前无学生信息，请添加后再查询");
        }

        //打印表头
        System.out.println("id\t\t姓名\t年龄\t家庭住址");

        //打印学生信息
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getHomeAddress());
        }
    }


    //判断id是否存在
    public static boolean contains(ArrayList<Student> list,String id){
        //循环得到Student对象
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            //得到id并进行判断
            if (stu.getId().equals(id)){//存在 ture
                return true;
            }
        }
        //不存在 false
        return false;
    }
    //获取索引
    public static int getIndex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            //判断id是否存在
            if (stu.getId().equals(id)){//存在，返回对应索引
                return i;
            }
        }
        return -1;  //不存在返回-1
    }


}
